package work.MrSun.xyq.util.isUpdate;


import work.MrSun.xyq.model.XyqMessage;
import work.MrSun.xyq.model.XyqUser;
import work.MrSun.xyq.service.XyqMessageDetailService;
import work.MrSun.xyq.service.XyqUserService;

public class IsUpdate {

    private Integer code;

    public Integer getCode() {

        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public IsUpdate isTrue(Integer id, Integer messageId, String message, XyqMessageDetailService xyqMessageDetailService, XyqUserService xyqUserService) {



        IsUpdate isUpdate = new IsUpdate();
        isUpdate.setCode(500);
        XyqUser user = xyqUserService.getById(id);

        if (user == null) {
            isUpdate.setCode(400);
            return isUpdate;
        }
        XyqMessage mangoMessage = new XyqMessage();

        if (user.getUserIsAdmin() == 2) {
            mangoMessage.setMessageId(messageId);
        } else {
            mangoMessage.setMessageId(messageId);
            mangoMessage.setUserId(id);

            Integer count = xyqMessageDetailService.findCount(mangoMessage);

            if (count == 0) {
                isUpdate.setCode(400);
                return isUpdate;
            }
        }
        mangoMessage.setMessageDetail(message);
        xyqMessageDetailService.update(mangoMessage);
        isUpdate.setCode(200);
        return isUpdate;
    }
}
